// Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.


#ifndef ELECTRON_SHELL_BROWSER_LIB_POWER_OBSERVER_OHOS_H_
#define ELECTRON_SHELL_BROWSER_LIB_POWER_OBSERVER_OHOS_H_

#include <string>

#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/power_monitor/power_observer.h"
#include "base/files/scoped_file.h"

namespace electron {

class PowerObserverOhos {
 public:
  explicit PowerObserverOhos(base::PowerSuspendObserver* suspend_observer);
  ~PowerObserverOhos();

  // disable copy
  PowerObserverOhos(const PowerObserverOhos&) = delete;
  PowerObserverOhos& operator=(const PowerObserverOhos&) = delete;

  void SetShutdownHandler(base::RepeatingCallback<bool()> should_shutdown);
  void OnPrepareForSleep(std::string event);

 private:
  void BlockSleep();
  void UnblockSleep();
  void BlockShutdown();
  void UnblockShutdown();

  void OnLoginServiceAvailable(bool available);
  void OnSignalConnected(const std::string& interface,
                         const std::string& signal,
                         bool success);

  base::RepeatingCallback<bool()> should_shutdown_;
  base::PowerSuspendObserver* suspend_observer_ = nullptr;

  std::string lock_owner_name_;
  base::ScopedFD sleep_lock_;
  base::ScopedFD shutdown_lock_;
  base::WeakPtrFactory<PowerObserverOhos> weak_ptr_factory_{this};
};

}  // namespace electron

#endif  // ELECTRON_SHELL_BROWSER_LIB_POWER_OBSERVER_OHOS_H_
